package cdms.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import cdms.web.SenderInterceptor;
import cdms.web.ManagerInterceptor;

/**
 * 
 * The configuration for webMVC
 * 
 * @author fengduxin
 * @version 1.0
 *
 */
@Configuration
@EnableWebMvc
@ComponentScan("cdms.web")
public class WebConfig extends WebMvcConfigurerAdapter {

	/**
	 * Custom view resolver
	 * 
	 * @return
	 */
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	/**
	 * register a default Handler: DefaultServletHttpRequestHandler
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	/**
	 * Define static resource processing
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
		super.addResourceHandlers(registry);
	}

	/**
	 * Define intercepter
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		registry.addInterceptor(new SenderInterceptor()).addPathPatterns(new String[] { "/sender", "/sender/**" })// 添加拦截
				.excludePathPatterns("/sender/login");// excludePathPatterns排除拦截

		registry.addInterceptor(new ManagerInterceptor()).addPathPatterns(new String[] { "/manager", "/manager/**" })// 添加拦截
				.excludePathPatterns("/manager/login");// excludePathPatterns排除拦截

		super.addInterceptors(registry);
	}

}
